<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AlbumController extends BaseController
{
    /**
     * 专题展示
     */
    public function show(Request $request, $spid, $termid, $album_id)
    {
        // 设备ID
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        // 专题ID
        if (empty($album_id)) {
            return response()->json(['status' => '404', 'data' => '专题不能为空!']);
        }
        // 获取专题信息
        $album = DB::table('albums')->where(['album_id' => $album_id])->first();
        if (empty($album)) {
            return response()->json(['status' => '404', 'data' => '没有找到此专题!']);
        }

        $data = [];
        // 媒资信息
        $vasseti       = [];
        $albumVassetis = [];
        // 专题ID
        $data['rowid'] = $album->album_id;
        // 专题名称
        $data['name']       = $album->name;
        // 标题
        $data['title'] = $album->title;
        // 副标题
        $data['subtitle'] = $album->subtitle;
        // 简述
        $data['short_desc'] = $album->short_desc;
        // 详情
        $data['long_desc'] = $album->long_desc;
        // FTP 配置
        $ftpUrl = config('app.host.ftp');
        // 图片
        $data['img'] = $ftpUrl . $album->backimg;
        // 背景图片
        $data['background_img'] = $ftpUrl . $album->backimg;
	$data['subtitle'] = $album->subtitle;
        $vassetis         = json_decode($album->vassetis, 1);

        if (empty($vassetis)) {
            $data['recommend'] = (object) [];
            $data['data']      = [];
        } else {
            foreach ($vassetis as $key => $value) {
                 $vassetid = $value['id'];
		 // 媒资ID
                $vasseti['vasseti_id'] = $vassetid;
                $vassetis              = DB::table('vassetis')->where(['vasseti_id' => $vassetid])->first();
                if (!empty($vassetis)) {
                    // 标题
                    $vasseti['title'] = $vassetis->title;
                    // 副标题
                    $vasseti['subtitle'] = $vassetis->subtitle;
                    // 评分
                    $vasseti['score'] = $vassetis->score;
                    // 详情
                    $vasseti['long_desc'] = $vassetis->long_desc;
                    // 简述
                    $vasseti['short_desc'] = $vassetis->short_desc;
                    // 图片
                    $img = json_decode($vassetis->imgs, 1);
                    if (empty($img)) {
                        $vasseti['img'] = (object) [];
                    } else {
                        foreach ($img as $imgKey => $imgValue) {
                            $imgHref = image_match($imgValue['href'], 'SIMG');
                            if ($imgHref) {
                                $vasseti['img'] = json_decode(json_encode(['href' => $ftpUrl . $imgHref]));
                                break;
                            } else {
                                $vasseti['img'] = (object) [];
                            }
                        }
                    }
                } else {
                    // 标题
                    $vasseti['title'] = null;
                    // 副标题
                    $vasseti['subtitle']   = null;
                    $vasseti['score']      = null;
                    $vasseti['long_desc']  = null;
                    $vasseti['short_desc'] = null;
                    // 分类
                    $vasseti['tag'] = null;
                    // 图片
                    $vasseti['img'] = (object) [];
                }
                // 获取焦点事件
                $vasseti['onclick']['route']["tplType"] = "DETAIL";
                $vasseti['onclick']['route']["tplID"]   = "";
                $vasseti['onclick']['href']             = url("/{$spid}/{$termid}/vasset/{$vassetid}/meta");
                $albumVassetis[]                        = $vasseti;
            }
            if (empty($albumVassetis)) {
               $data['data'] = [];
               $data['recommend'] = (object)[];
            }else{
                $data['recommend'] = array_shift($albumVassetis);
                $data['data'] = $albumVassetis;
            }
        }
        return response()->json(['status' => '200', 'data' => $data]);
    }

    public function index()
    {
    }
}
